﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;

namespace SendMail_HelloWorld
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static void SendMail(string Body)
        {
            MailMessage SendEMail = new MailMessage();
            SendEMail.To.Add(new MailAddress("hackerafan@vip.qq.com", "hackerafan", System.Text.Encoding.UTF8));
            SendEMail.From = new MailAddress("337957183@qq.com","337957183",System.Text.Encoding.UTF8);
            SendEMail.Subject = "C# SendMail_HelloWorld Test Windows Forms Application";
            SendEMail.SubjectEncoding = System.Text.Encoding.UTF8;
            SendEMail.Body = Body;
            SendEMail.BodyEncoding = System.Text.Encoding.UTF8;
            SendEMail.IsBodyHtml = true;
            SendEMail.Priority = MailPriority.High;
            SmtpClient sc = new SmtpClient();
            sc.Host = "smtp.qq.com";
            sc.Port = 25;
            sc.UseDefaultCredentials = true;
            sc.Credentials = new System.Net.NetworkCredential("337957183", "admin");
            sc.DeliveryMethod = SmtpDeliveryMethod.Network;
            object userState = SendEMail; 
            try
            {
                sc.Send(SendEMail);
            MessageBox.Show("Send Ok!!  Thanks for your submit!!");
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.Message, "Send Error!!");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("You must enter a name.", "Name Entry Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (textBox2.Text=="")
                {
                    MessageBox.Show("You must enter the message .", "Message Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                } 
                else
                {
                    DialogResult ResultName;
                    ResultName = MessageBox.Show("You are  " + textBox1.Text, "Prompt", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                    if (ResultName == DialogResult.Yes)
                    {
                        DialogResult ResultMessage;
                        ResultMessage = MessageBox.Show("You want to say  " + textBox2.Text, "Prompt", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                        if (ResultMessage==DialogResult.Yes)
                        {
                            SendMail("Name:  " + textBox1.Text + "。 <br/>   Message:  " + textBox2.Text + "。");
                        } 
                        else
                        {
                            MessageBox.Show("Oh!!   You deceive  me!!");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Oh!!   You deceive  me!!");
                    }
                    textBox1.Text = "";
                    textBox2.Text = "";
                }
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("tencent://message/?uin=337957183");
        }
    }
}